package com.zycfc.zsf.boot.util.http;

import com.zycfc.zsf.boot.util.json.*;
import org.apache.http.entity.*;
import org.apache.http.util.*;
import java.io.*;
import org.apache.http.*;
import org.apache.http.message.*;
import org.apache.http.client.entity.*;
import java.util.*;
import org.apache.http.impl.client.*;
import org.apache.http.client.methods.*;
import org.slf4j.*;

public class HttpClientUtils
{
    private static final Logger LOGGER;
    public static final String CHAR_SET = "UTF-8";
    public static final String CONTEXT_TYPE = "application/json";
    public static final String FORM_CONTEXT_TYPE = "application/x-www-form-urlencoded";
    private static CloseableHttpClient client;
    private static CloseableHttpClient shortClient;
    
    public static <T> T postForObject(final String url, final Object request, final Class<T> responseType) {
        CloseableHttpResponse response = null;
        try {
            final HttpPost post = new HttpPost(url);
            if (null != request) {
                final String reqData = (request instanceof String) ? request.toString() : JsonUtils.objectToJson(request);
                final HttpEntity entity = (HttpEntity)new StringEntity(reqData, ContentType.create("application/json", "UTF-8"));
                post.setEntity(entity);
            }
            response = HttpClientUtils.client.execute((HttpUriRequest)post);
            final String respData = EntityUtils.toString(response.getEntity());
            return (T)((responseType == String.class) ? respData : JsonUtils.jsonToObject(respData, responseType));
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            if (null != response) {
                try {
                    response.close();
                }
                catch (IOException e2) {
                    HttpClientUtils.LOGGER.error("Close Exception:", (Throwable)e2);
                }
            }
        }
    }
    
    public static <T> T postForObject(final String url, final Object request, final Map<String, String> headers, final Class<T> responseType) {
        CloseableHttpResponse response = null;
        try {
            final HttpPost post = new HttpPost(url);
            if (null != request) {
                final String reqData = (request instanceof String) ? request.toString() : JsonUtils.objectToJson(request);
                final HttpEntity entity = (HttpEntity)new StringEntity(reqData, ContentType.create("application/json", "UTF-8"));
                post.setEntity(entity);
            }
            if (headers != null && headers.size() > 0) {
                for (final Map.Entry<String, String> entry : headers.entrySet()) {
                    post.addHeader((String)entry.getKey(), (String)entry.getValue());
                }
            }
            response = HttpClientUtils.client.execute((HttpUriRequest)post);
            final String respData = EntityUtils.toString(response.getEntity());
            return (T)((responseType == String.class) ? respData : JsonUtils.jsonToObject(respData, responseType));
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            if (null != response) {
                try {
                    response.close();
                }
                catch (IOException e2) {
                    HttpClientUtils.LOGGER.error("Close Exception:", (Throwable)e2);
                }
            }
        }
    }
    
    public static <T> T postByMap(final String url, final Map<String, String> requestMap, final Class<T> responseType) {
        return postByMap(url, requestMap, null, responseType);
    }
    
    public static <T> T postByMap(final String url, final Map<String, String> requestMap, final Map<String, String> headers, final Class<T> responseType) {
        return postByMap(url, requestMap, headers, responseType, "application/json");
    }
    
    public static <T> T postByMap(final String url, final Map<String, String> requestMap, final Map<String, String> headers, final Class<T> responseType, final String contextType) {
        CloseableHttpResponse response = null;
        try {
            final HttpPost post = new HttpPost(url);
            if (headers != null) {
                for (final String key : headers.keySet()) {
                    final String value = headers.get(key);
                    post.addHeader(key, value);
                }
            }
            if (null != requestMap) {
                final Iterator<Map.Entry<String, String>> iterator = requestMap.entrySet().iterator();
                final List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
                while (iterator.hasNext()) {
                    final Map.Entry<String, String> entry = iterator.next();
                    nvpList.add((NameValuePair)new BasicNameValuePair((String)entry.getKey(), (String)entry.getValue()));
                }
                final UrlEncodedFormEntity entity = new UrlEncodedFormEntity((List)nvpList, "UTF-8");
                entity.setContentType("application/x-www-form-urlencoded".equals(contextType) ? "application/x-www-form-urlencoded" : "application/json");
                post.setEntity((HttpEntity)entity);
            }
            response = HttpClientUtils.client.execute((HttpUriRequest)post);
            final String respData = EntityUtils.toString(response.getEntity());
            return (T)((responseType == String.class) ? respData : JsonUtils.jsonToObject(respData, responseType));
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            if (null != response) {
                try {
                    response.close();
                }
                catch (IOException e2) {
                    HttpClientUtils.LOGGER.error("Close Exception:", (Throwable)e2);
                }
            }
        }
    }
    
    public static <T> T getForObject(final String url, final Map<String, String> headers, final Class<T> responseType) {
        CloseableHttpResponse response = null;
        try {
            final HttpGet httpGet = new HttpGet(url);
            if (headers != null) {
                for (final String key : headers.keySet()) {
                    final String value = headers.get(key);
                    httpGet.addHeader(key, value);
                }
            }
            response = HttpClientUtils.client.execute((HttpUriRequest)httpGet);
            final String respData = EntityUtils.toString(response.getEntity());
            return (T)((responseType == String.class) ? respData : JsonUtils.jsonToObject(respData, responseType));
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            if (null != response) {
                try {
                    response.close();
                }
                catch (IOException e2) {
                    HttpClientUtils.LOGGER.error("Close Exception:", (Throwable)e2);
                }
            }
        }
    }
    
    public static <T> T getForObject(final String url, final Class<T> responseType) {
        CloseableHttpResponse response = null;
        try {
            response = HttpClientUtils.client.execute((HttpUriRequest)new HttpGet(url));
            final String respData = EntityUtils.toString(response.getEntity());
            return (T)((responseType == String.class) ? respData : JsonUtils.jsonToObject(respData, responseType));
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            if (null != response) {
                try {
                    response.close();
                }
                catch (IOException e2) {
                    HttpClientUtils.LOGGER.error("Close Exception:", (Throwable)e2);
                }
            }
        }
    }
    
    public static <T> T putForObject(final String url, final Class<T> responseType) {
        final CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        final HttpPut httpput = new HttpPut(url);
        httpput.setHeader("Content-type", "application/json");
        CloseableHttpResponse response = null;
        try {
            response = closeableHttpClient.execute((HttpUriRequest)httpput);
            final int status = response.getStatusLine().getStatusCode();
            if (status != 200) {
                throw new RuntimeException("\u8bf7\u6c42\u7ed3\u679c\u72b6\u6001:" + status + "\uff0c\u8bf7\u6c42\u5931\u8d25");
            }
            return JsonUtils.jsonToObject(EntityUtils.toString(response.getEntity()), responseType);
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            if (null != response) {
                try {
                    response.close();
                }
                catch (IOException e2) {
                    HttpClientUtils.LOGGER.error("Close Exception:", (Throwable)e2);
                }
            }
        }
    }
    
    public static <T> T postByShortConn(final String url, final Object request, final Class<T> responseType) {
        CloseableHttpResponse response = null;
        final CloseableHttpClient client = null;
        try {
            final HttpPost post = new HttpPost(url);
            if (null != request) {
                final String reqData = (request instanceof String) ? request.toString() : JsonUtils.objectToJson(request);
                final HttpEntity entity = (HttpEntity)new StringEntity(reqData, ContentType.create("application/json", "UTF-8"));
                post.setEntity(entity);
            }
            response = HttpClientUtils.shortClient.execute((HttpUriRequest)post);
            final String respData = EntityUtils.toString(response.getEntity());
            return (T)((responseType == String.class) ? respData : JsonUtils.jsonToObject(respData, responseType));
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            if (null != response) {
                try {
                    response.close();
                }
                catch (IOException e2) {
                    HttpClientUtils.LOGGER.error("Close Exception:", (Throwable)e2);
                }
            }
        }
    }
    
    public static <T> T postMapByShortConn(final String url, final Map<String, String> requestMap, final Map<String, String> headers, final Class<T> responseType, final String contextType) {
        CloseableHttpResponse response = null;
        final CloseableHttpClient client = null;
        try {
            final HttpPost post = new HttpPost(url);
            if (headers != null) {
                for (final String key : headers.keySet()) {
                    final String value = headers.get(key);
                    post.addHeader(key, value);
                }
            }
            if (null != requestMap) {
                final Iterator<Map.Entry<String, String>> iterator = requestMap.entrySet().iterator();
                final List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
                while (iterator.hasNext()) {
                    final Map.Entry<String, String> entry = iterator.next();
                    nvpList.add((NameValuePair)new BasicNameValuePair((String)entry.getKey(), (String)entry.getValue()));
                }
                final UrlEncodedFormEntity entity = new UrlEncodedFormEntity((List)nvpList, "UTF-8");
                entity.setContentType("application/x-www-form-urlencoded".equals(contextType) ? "application/x-www-form-urlencoded" : "application/json");
                post.setEntity((HttpEntity)entity);
            }
            response = HttpClientUtils.shortClient.execute((HttpUriRequest)post);
            final String respData = EntityUtils.toString(response.getEntity());
            return (T)((responseType == String.class) ? respData : JsonUtils.jsonToObject(respData, responseType));
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            if (null != response) {
                try {
                    response.close();
                }
                catch (IOException e2) {
                    HttpClientUtils.LOGGER.error("Close Exception:", (Throwable)e2);
                }
            }
        }
    }
    
    static {
        LOGGER = LoggerFactory.getLogger((Class)HttpClientUtils.class);
        HttpClientUtils.client = null;
        HttpClientUtils.shortClient = null;
        if (null == HttpClientUtils.client) {
            try {
                HttpClientUtils.client = DefaultHttpClientBuilder.httpClient();
                HttpClientUtils.shortClient = DefaultHttpClientBuilder.getNoReuseHttpClient();
            }
            catch (Exception e) {
                HttpClientUtils.LOGGER.error("\u83b7\u53d6HttpClient\u5931\u8d25");
            }
        }
    }
}
